package com.bazinga.contact.view;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.model.SearchUser;
import com.bazinga.contact.model.SingleJob;
import com.opensymphony.xwork2.ActionSupport;

public class SearchApplicantAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private static final String dbServer = "127.0.0.1";
	@SuppressWarnings("unused")
	private Map<String, Object> session;
	private String skills;
	private String major;
	private String startTime;
	private String endTime;
	private List<SearchUser> searchResults;


	
	public String execute() {
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		
		String searchQuery = "select u.userid,u.fname, u.lname, sk.skill1, sk.skill2, sk.major from userdetails u, studentskills sk where u.userid=sk.userid";
		if(!major.equals("")){
			searchQuery += " AND sk.major ='"+major+"'";
		}
		if(!skills.equals("")){
			searchQuery += " AND (sk.skill1 ='"+skills+"' OR sk.skill2 ='"+skills+"' OR sk.skill3 ='"+skills+"' OR sk.skill4 ='"+skills+"')";
		}
		searchQuery+=";";
		
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		
		try {
			rs = stmt.executeQuery(searchQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		
		searchResults = new ArrayList<SearchUser>();
		try {
			while(rs.next()) {
				SearchUser temp = new SearchUser();
				temp.setFname(rs.getString("fname"));
				temp.setLname(rs.getString("lname"));
				temp.setUserid(new Long(rs.getString("userid")));
				if(rs.getString("major").equals("1"))
					temp.setMajor("Computer Science");
				else if(rs.getString("major").equals("2"))
					temp.setMajor("Physical Sciences");
				else if(rs.getString("major").equals("3"))
					temp.setMajor("Social Sciences");
				else if(rs.getString("major").equals("4"))
					temp.setMajor("Other");
				
				if(rs.getString("skill1").equals("1"))
					temp.setSkills("Web Development");
				else if(rs.getString("skill1").equals("2"))
					temp.setSkills("Java");
				else if(rs.getString("skill1").equals("3"))
					temp.setSkills("Library Management");
				else if(rs.getString("skill1").equals("4"))
					temp.setSkills("Cashier");
				else if(rs.getString("skill1").equals("5"))
					temp.setSkills("Chef");
				
				if(rs.getString("skill2").equals("1"))
					temp.setSkills(temp.getSkills()+", Web Development");
				else if(rs.getString("skill2").equals("2"))
					temp.setSkills(temp.getSkills()+", Java");
				else if(rs.getString("skill2").equals("3"))
					temp.setSkills(temp.getSkills()+", Library Management");
				else if(rs.getString("skill2").equals("4"))
					temp.setSkills(temp.getSkills()+", Cashier");
				else if(rs.getString("skill2").equals("5"))
					temp.setSkills(temp.getSkills()+", Chef");
				
				searchResults.add(temp);
			}
		}
		
		catch(SQLException ex) {
			ex.printStackTrace();
		}
		
		catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return "success";
	}
    public String getSkills() {
		return skills;
	}
	public void setSkills(String skills) {
		this.skills = skills;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public List<SearchUser> getSearchResults() {
		return searchResults;
	}
	public void setSearchResults(List<SearchUser> searchResults) {
		this.searchResults = searchResults;
	}
	public SearchApplicantAction() {
        super();
        // TODO Auto-generated constructor stub
    }
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

}
